//
//  SetupUITableViewCell.m
//  ent
//
//  Created by Tyrion on 5/22/15.
//  Copyright (c) 2015 ND. All rights reserved.
//

#import "SetupUITableViewCell.h"
#import <Masonry/Masonry.h>
#import <UIImageView+WebCache.h>


@implementation SetupUITableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self constructView];
        return self;
    }else{
        return nil;
    }
}

-(void)constructView{
    
    
//    self.layer.borderWidth = 1;
    
    CGFloat height = CGRectGetHeight(self.frame);
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.placeHolderImage = [UIImage imageNamed:@"portrait"];
    
//    self.placeHolderImage = [UIImage new];
//    self.portraitLayer = [CALayer layer];
//    self.portraitLayer.frame = CGRectMake(10, 20, 54, 54);
//    self.portraitLayer.contents = (__bridge id)(self.placeHolderImage.CGImage);
//    [self.layer addSublayer: self.portraitLayer];
//    CALayer *maskLayer = [CALayer layer];
//    maskLayer.frame = self.portraitLayer.bounds;
//    maskLayer.contents = (__bridge id)([UIImage imageNamed:@"mask"].CGImage);
//    self.portraitLayer.mask = maskLayer;
//    self.portraitLayer.hidden = YES;
    
    self.portraitImageView = [UIImageView new];
    self.portraitImageView.frame = CGRectMake(10, 20, 54, 54);
    [self.contentView addSubview: self.portraitImageView];
    self.portraitImageView.hidden = YES;
    CALayer *imageLayer = self.portraitImageView.layer;
    [imageLayer setCornerRadius: self.portraitImageView.frame.size.width/2];
    [imageLayer setMasksToBounds:YES];
    
    self.nameLabel = [[UILabel alloc] init];
    [self.nameLabel setText:@"  "];
    [self.nameLabel setFont:[UIFont systemFontOfSize:15]];
    [self.contentView addSubview: self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.equalTo(self.mas_left).offset(15);
        make.top.equalTo(self.mas_top).offset(0);
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.size.mas_equalTo(CGSizeMake(70, height));
    }];
//    self.nameLabel.layer.borderWidth = 1;
    
    self.valueLabel = [UILabel new];
    [self.valueLabel setText: @" "];
    [self.valueLabel setFont:[UIFont systemFontOfSize:15]];
    [self.contentView addSubview: self.valueLabel];
    [self.valueLabel mas_makeConstraints:^(MASConstraintMaker *make){
        make.centerY.equalTo(self.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-15 - 13 - 13);
        make.height.equalTo(@28);
        make.left.equalTo(self.nameLabel.mas_right).offset(20);
//        make.width.equalTo();
    }];
//    self.valueLabel.layer.borderWidth = 1;
    self.textLabel.textAlignment = NSTextAlignmentRight;
//    self.textLabel.layer.borderWidth = 1;
    
    self.enterImageView = [[UIImageView alloc] init];
    self.enterImageView.image = [UIImage imageNamed:@"enter"];
    [self.contentView addSubview: self.enterImageView];
    [self.enterImageView mas_makeConstraints:^(MASConstraintMaker *make){
        make.right.equalTo(self.mas_right).offset(-15);
        make.top.equalTo(self.valueLabel.mas_top).offset(5);
        make.bottom.equalTo(self.valueLabel.mas_bottom).offset(-5);
    }];
}


-(void)setItem:(SetupData*)data{

    if ((data != nil)&& (data.portrait != nil)) {
        NSURL *url = [[NSURL alloc] initWithString:data.portrait];
        
        [self.portraitImageView sd_setImageWithURL:url placeholderImage:self.placeHolderImage];
    }

    return;
}

-(void)prepareForReuse{
    //todo
    
    [self.nameLabel setText:@" "];
//    self.portraitView.image = nil;
    [self.valueLabel setText:@" "];
    self.portraitLayer.contents = nil;
    
}

@end
